package com.exeption;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.qiniu.common.QiniuException;
import living.com.result.ApiResult;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.lang.reflect.UndeclaredThrowableException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * @author zdh
 */
@ControllerAdvice
public class WebExceptionHandler {

    /**
     * 非法参数异常
     */
    @ResponseStatus()
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public ApiResult illegalArgumentException(IllegalArgumentException e) {
        return ApiResult.fail(HttpStatus.NOT_ACCEPTABLE.name(), e.getMessage()==null?"系统异常":e.getMessage());
    }

    /**
     * JSR303数据校验异常
     */
    @ResponseStatus()
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ApiResult methodArgumentNotValidException(MethodArgumentNotValidException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("错误信息: ");
        fieldErrors.forEach(fieldError -> {
            stringBuilder.append(fieldError.getDefaultMessage())
                    .append(", ");
        });
        return ApiResult.fail(HttpStatus.NOT_ACCEPTABLE.name(),
                 stringBuilder.toString());
    }



    @ResponseStatus()
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ApiResult exception(Exception e) {
        return ApiResult.fail(HttpStatus.INTERNAL_SERVER_ERROR.name(), e.getMessage()==null?"系统异常":e.getMessage());
    }

    @ResponseStatus()
    @ExceptionHandler(CannotCreateTransactionException.class)
    @ResponseBody
    public ApiResult cannotCreateTransactionException(CannotCreateTransactionException e) {
        return ApiResult.fail(HttpStatus.INTERNAL_SERVER_ERROR.name(), "数据库连接超时");
    }

    @ResponseStatus()
    @ExceptionHandler(QiniuException.class)
    @ResponseBody
    public ApiResult qiniuException(QiniuException e) {
        return ApiResult.fail(HttpStatus.INTERNAL_SERVER_ERROR.name(), "文件处理出现异常: "+e.getMessage());
    }


    @ResponseStatus()
    @ExceptionHandler(JsonProcessingException.class)
    @ResponseBody
    public ApiResult jsonProcessingException(JsonProcessingException e) {
        return ApiResult.fail(HttpStatus.INTERNAL_SERVER_ERROR.name(), "json序列化出现异常");
    }


    /**
     * 唯一索引重复异常
     */
    @ResponseStatus()
    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public ApiResult duplicateKeyException(DuplicateKeyException e) {
        String errorInfo = e.getCause().getMessage().split(" ")[2];
        return ApiResult.fail(HttpStatus.INTERNAL_SERVER_ERROR.name(), errorInfo+"重复,请重新选择");
    }

    @ResponseStatus()
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public ApiResult nullPointerException(NullPointerException e) {
        return ApiResult.fail(HttpStatus.NOT_FOUND.name(), "资源不存在");
    }


}
